<?php
namespace app\api\controller;
use app\common\BaseController;
use app\api\model\Invoices as InvoicesModel;
use app\api\model\Team as TeamModel;
use app\api\model\TeamIncome as TeamIncomeModel;
use app\api\model\Receipt as ReceiptModel;
use app\api\model\Single as SingleModel;
use app\api\model\Guest as GuestModel;
use app\api\model\InInvoices as IninvoicesModel;
use app\api\model\Client as ClientModel;

use think\Db;
use app\common\Logs;
class Invoices extends BaseController{    
    public function __construct(InvoicesModel $invoicesModel){
        parent::__construct();
        $this->model = $invoicesModel;
    }
    public function searchBySn(){
        $params = $this->request->param();
        $params['sn'] = trim($params['sn']);
        $teamModel = new TeamModel();
        $res = $teamModel->where('sn',$params['sn'])->find();
        if($res){
            $teamIncomeModel = new TeamIncomeModel();
            $data = $teamIncomeModel->where(['type'=>$res['type'],'parent_id'=>$res['id']])->select();  
            $list=[];
            foreach ($data as $v) {
                $totalmoney= $v['num']* $v['sell_price'];
                $invoicesModel = new IninvoicesModel();
                $invoices_data = $invoicesModel->where(["income_id"=>$v["id"],"type"=>$v['type']])->select();
                $invoices = 0;
                foreach ($invoices_data as $x) {
                    $invoices += $x['invoice_price'];
                }
                $guest = new GuestModel();
                $guestinfo = $guest->where('id',$v['guest_id'])->find();
                $client = new ClientModel();
                $clientinfo = $client->where('id',$v['client_id'])->find();
                $temp=[
                    'producttype'=>'team',
                    'sn'=>$res['sn'],
                    'title'=>$res['title'],
                    'type'=>$v['type'],
                    'id' => $v['id'],
                    'parentid' => $res['id'],
                    'guest' => $guestinfo?$guestinfo['guest_name']:'',
                    'client' => $clientinfo?$clientinfo['company_name']:'',
                    'num'=>$v['num'],
                    'totalmoney'=>$totalmoney,                    
                    'invoiced'=>$invoices,
                    'uninvoiced'=>$totalmoney-$invoices
                ]; 
                array_push($list,$temp);
            } 
                 
            return $list;
        }
        $singleModel = new SingleModel();
        $data = $singleModel->where('sn',$params['sn'])->select();
        if($data){           
            $list=[];
            foreach ($data as $v) {
                $totalmoney= $v['num']* $v['sell_price'];
                $invoicesModel = new IninvoicesModel();
                $invoices_data = $invoicesModel->where(["income_id"=>$v["id"],"type"=>$v['type']])->select();
                $invoices = 0;
                foreach ($invoices_data as $x) {
                    $invoices += $x['invoice_price'];
                }
                $guest = new GuestModel();
                $guestinfo = $guest->where('id',$v['guest_id'])->find();
                $client = new ClientModel();
                $clientinfo = $client->where('id',$v['client_id'])->find();
                $temp=[
                    'producttype'=>'single',
                    'sn'=>$v['sn'],
                    'title'=>$v['product_name'],
                    'type'=>$v['type'],
                    'id' => $v['id'],
                    'parentid' => $v['id'],
                    'guest' => $guestinfo?$guestinfo['guest_name']:'',
                    'client' => $clientinfo?$clientinfo['company_name']:'',
                    'num'=>$v['num'],
                    'totalmoney'=>$totalmoney,                    
                    'invoiced'=>$invoices,
                    'uninvoiced'=>$totalmoney-$invoices
                ]; 
                array_push($list,$temp);
            }   
            return $list;
        }   
        return [];     
    } 

    public function get_new(){       
        $data = $this->model->where('isread',0)->select();       
        return $data;
    }

    public function save_invoice(){
        $param = $this->request->param();
        $params = json_decode($param['params'],true);  
        $params['datas'] = json_encode($params['datas'],JSON_UNESCAPED_UNICODE);         
        $params['operator_id'] = $param['operator_id'];
        $data = $this->model->allowField(true)->isUpdate(false)->save($params);
        if($data){
            Logs::add("新增发票记录",$params['operator_id'],"发票", null,json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        } 
        return $this->model;
    }

   //支出审核
   public function review(){        
        $param = $this->request->param();        
        $id = $param['id'];     
        $invoice_no = $param['invoice_no'];    
        $invoice_date = $param['invoice_date']; 
        $operator_id = $param['operator_id'];
        $old = $this->model->where('id',$id)->find();
      
        $new=$this->model->where('id',$id)->update(['is_review'=>'1','isread'=>1,'invoice_no'=>$invoice_no]); 
        if($new){
            $list = json_decode($old['datas'],true);  
            foreach($list as $v){
                $temp = [
                    "invoice_date"=>$invoice_date,
                    "invoice_no"=>$invoice_no,
                    "inout"=>0,
                    "type"=>$v['type'],
                    "income_id"=>$v['id'],
                    "company_name"=>$old['company_name'],
                    "invoice_content"=>$old['invoice_content'],
                    "invoice_price"=>$v['realmoney'],
                    "operator_id"=>$operator_id,
                    "invoice_id"=>$old['id'],
                    "is_check"=>1
                ];
                $invoicesModel = new IninvoicesModel();
                $data = $invoicesModel->allowField(true)->isUpdate(false)->save($temp);               
            }              
            Logs::add("审核发票申请记录",$operator_id,$invoice_no, json_encode($old,JSON_UNESCAPED_UNICODE),"");  
        }
        return $new;
    }
    //支出取消审核
    public function cancel_review(){
        $param = $this->request->param();        
        $id = $param['id'];        
        $operator_id = $param['operator_id'];
        $old = $this->model->where('id',$id)->find();
        $data=$this->model->where('id',$id)->update(['is_review'=>'0']);  
        if($data){
            $invoicesModel = new IninvoicesModel();
            $data = $invoicesModel->allowField(true)->where('invoice_id',$id)->delete();
            Logs::add("解除审核发票申请记录",$operator_id,$old['invoice_no'], json_encode($old,JSON_UNESCAPED_UNICODE),"");   
        }
        return $data;
    }

    public function index(){
        $params = $this->request->param();
        if(!empty($params['userId'])){
            $params['operator_id'] = $params['userId'];
        }
        $data = $this->model->allowField(true)->invoiceList($params);        
        return $data;
    } 
    public function keywordAll(){
        $params = $this->request->param();
        $data = $this->model->allowField(true)->keywordSearch($params);        
        return $data;
    }
     //添加
     public function create(){
        $param = $this->request->param();       
        $params = json_decode($param['params'],true);        
        $operator_id = json_decode($param['operator_id'],true);    
        $data = $this->model->allowField(true)->isUpdate(false)->save($params);
        if($data){
            Logs::add("新增发票记录",$params['operator_id'],"发票", null,json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        } 
        return $this->model;
    }
    //更新
    public function update(){
        $param = $this->request->param();       
        $params = json_decode($param['params'],true);        
        $operator_id = json_decode($param['operator_id'],true); 
        $old = $this->model->where("id",$params['id'])->find();     
        $data = $this->model->allowField(true)->isUpdate(true)->save($params);
        if($data){
            Logs::add("修改发票记录",$operator_id,"发票", json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        } 
        return $this->model;
    }
    //删除
    public function delete(){
        $params = $this->request->param();
        $old = $this->model->where("id",$params['id'])->find();   
        $data = $this->model->allowField(true)->where('id',$params['id'])->delete();
        if($data){
            Logs::add("删除发票记录",$params['operator_id'],"发票", json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        } 
        return $data;
    }

}
